package com.lxl.securitypermission.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxl.securitypermission.constant.HttpCode;
import com.lxl.securitypermission.constant.StatusCode;
import com.lxl.securitypermission.controller.vo.RolePermissionVo;
import com.lxl.securitypermission.controller.vo.SimpleRoleVo;
import com.lxl.securitypermission.dto.ResponseResult;
import com.lxl.securitypermission.dto.ResponseResultPage;
import com.lxl.securitypermission.entity.SRole;
import com.lxl.securitypermission.mapper.SPermissionMapper;
import com.lxl.securitypermission.mapper.SRoleMapper;
import com.lxl.securitypermission.mapper.SRolePermissionMapper;
import com.lxl.securitypermission.pojo.RolePermission;
import com.lxl.securitypermission.service.RoleService;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 绫小路
 * @Date 2020/12/31
 */
@Service
public class RoleServiceImpl implements RoleService {

  @Autowired
  private SRoleMapper sRoleMapper;
  @Autowired
  private SRolePermissionMapper sRolePermissionMapper;
  @Autowired
  private SPermissionMapper sPermissionMapper;

  @Override
  public List<String> getRoleList(Integer userId) {
    return sRoleMapper.getRoleList(userId);
  }

  @Override
  public List<RolePermission> getAllRolePermission() {
    return sRolePermissionMapper.getAllRolePermission();
  }

  @Override
  public ResponseResultPage getRoleLists(String search, int page, int limit) {
    Page pages = PageHelper.startPage(page, limit);
    List<SRole> result = sRoleMapper.getRoleLists(search);
    return new ResponseResultPage(HttpCode.OK, page, limit, pages.getTotal(), result);
  }

  @Override
  public ResponseResult add(String name, String description) {
    if (name.indexOf("ROLE_") == -1 || !StrUtil.isUpperCase(name.substring(5))) {
      return new ResponseResult(HttpCode.FAIL, "角色名称不正确，以 ROLE_ 开头，全大写");
    }
    SRole s = sRoleMapper.get(name, 1);
    if (s != null && s.getStatus() == StatusCode.VALID) {
      return new ResponseResult(HttpCode.FAIL, "已经存在的角色名: " + name);
    }
    SRole role = new SRole(name, description, StatusCode.VALID, new Date(), null);
    role.setName(name);
    role.setDescription(description);
    role.setCreateTime(new Date());
    role.setStatus(StatusCode.VALID);
    sRoleMapper.insertSelective(role);
    return new ResponseResult(HttpCode.OK, "添加成功！");
  }

  @Override
  public ResponseResult delete(String name) {
    sRoleMapper.delete(name);
    return new ResponseResult(HttpCode.OK, "删除角色成功！");
  }

  @Override
  public ResponseResult edit(Integer id, String name, String description) {
    if (name.indexOf("ROLE_") == -1 || !StrUtil.isUpperCase(name.substring(5))) {
      return new ResponseResult(HttpCode.FAIL, "角色名称不正确，以 ROLE_ 开头，全大写");
    }
    sRoleMapper.update(id,name, description);
    return new ResponseResult(HttpCode.OK, "修改成功！");
  }

  @Override
  public ResponseResultPage permissionList(String search, int page, int limit) {
    Page pages = PageHelper.startPage(page, limit);
    List<RolePermissionVo> result = sRoleMapper.getRolePermission(search);
    return new ResponseResultPage(HttpCode.OK, page, limit, pages.getTotal(), result);
  }

  @Override
  public ResponseResult deleteRolePermission(Integer id) {
    sRolePermissionMapper.deleteByPrimaryKey(id);
    return new ResponseResult(HttpCode.OK, "删除成功！");
  }

  @Override
  public List<SimpleRoleVo> getSimpleRoleVo(int status) {
    return sRoleMapper.getSimpleRoleVo(status);
  }

  @Override
  public ResponseResult add(Integer roleId, Integer permissionId) {
    Assert.isFalse(sRoleMapper.selectByPrimaryKey(roleId) == null, "不存在的角色ID");
    Assert.isFalse(sPermissionMapper.selectByPrimaryKey(permissionId) == null, "不存在的权限ID");
    Assert.isFalse(sRolePermissionMapper.hasExists(roleId, permissionId) > 0, "已经配置该权限");
    sRolePermissionMapper.addPermission(roleId, permissionId);
    return new ResponseResult(HttpCode.OK, "添加成功！");
  }
}
